/**
    Copyright (c) 2009 yakiimo02
    Distributed under the New BSD License.
    See included license.txt or http://www.yakiimo3d.com/NewBSDLicense.txt
**/

#include "DXUT.h"
#include "BitonicSortDemo.h"

/**
    Initialize member variables for a bitonic sort.
    @param bSortUp            [in]            sort in increasing order
    @param nElemPerFrame    [in]            number of elements to sort per frame
*/
void BitonicSortDemo::SortBegin( bool bSortUp, int nElemPerFrame )
{
    m_nMergeSize = 2;
    m_nMergeSubSize = m_nMergeSize>>1;
    m_nElem = 0;
    m_bSortUp = bSortUp;
    m_nElemPerFrame = nElemPerFrame;
}

/**
*/
void BitonicSortDemo::Update()
{
    Sort( m_data.size() );
}

/**
    @param nCount    [in]        The number of elements to sort.
*/
void BitonicSortDemo::Sort( int nCount )
{
    if( m_nElem == NUM_ELEMENTS ) {
        if( m_bSortUp ) {
            CheckSortUp( m_data );
        }
        else {
            CheckSortDown( m_data );
        }
        return;
    }

    // continue from where we stopped sorting
    int nMergeSize = m_nMergeSize;
    int nMergeSubSize = m_nMergeSubSize;
    int nElem = m_nElem;

    int nElemProcessed = 0;

    // loop from Merge( 2 ) to Merge( nCount )
    for( ; nMergeSize<=nCount; nMergeSize=nMergeSize*2 ) 
    {
        if( nElemProcessed != 0 ) {
            nMergeSubSize = nMergeSize>>1;
        }

        // Merge( nCount ) requires log2( nCount ) merges. Merge( nCount/2 ) -> Merge( 2 )
        for( ; nMergeSubSize>0; nMergeSubSize=nMergeSubSize>>1 ) 
        {
            if( nElemProcessed != 0 ) {
                nElem = 0;
            }

            // compare and swap elements
            for( ; nElem<nCount; ++nElem ) 
            {
                int nSwapElem = nElem^nMergeSubSize;
                // check to make sure to only swap once
                if( nSwapElem > nElem ) 
                {
                    if( m_bSortUp ) {
                        // sort in increasing order
                        if ( ( nElem & nMergeSize ) == 0 && m_data[ nElem ]>m_data[ nSwapElem ] ) { 
                            Swap( nElem, nSwapElem ); 
                        }
                        // sort in descending order
                        if ( ( nElem & nMergeSize ) != 0 && m_data[ nElem ]<m_data[ nSwapElem ] ) { 
                            Swap( nElem, nSwapElem ); 
                        }
                    }
                    else {
                        // sort in increasing order
                        if ( ( nElem & nMergeSize ) == 0 && m_data[ nElem ]<m_data[ nSwapElem ] ) { 
                            Swap( nElem, nSwapElem ); 
                        }
                        // sort in descending order
                        if ( ( nElem & nMergeSize ) != 0 && m_data[ nElem ]>m_data[ nSwapElem ] ) { 
                            Swap( nElem, nSwapElem ); 
                        }
                    }
                    ++nElemProcessed;
                }

                if( nElemProcessed > m_nElemPerFrame ) {
                    ++nElem;
                    break;
                }
            }
            
            if( nElemProcessed > m_nElemPerFrame ) {
                if( nElem == nCount ) {
                    nMergeSubSize=nMergeSubSize>>1;
                }
                break;
            }
        }

        if( nElemProcessed > m_nElemPerFrame ) {
            if( nMergeSubSize <= 0 ) {
                nMergeSize=nMergeSize*2;
            }
            break;
        }
    }

    // save where we stopped sorting
    m_nMergeSize = nMergeSize;
    m_nMergeSubSize = nMergeSubSize;
    m_nElem = nElem;
}

